package com.xyz.apis.modules.api.controller;


import com.alibaba.fastjson.JSONObject;
import com.xyz.admin.common.entity.EvUserEntity;
import com.xyz.apis.common.annotation.Idempotent;
import com.xyz.apis.common.utils.CommonUtil;
import com.xyz.apis.common.utils.R;
import com.xyz.apis.common.utils.Rest;
import com.xyz.apis.config.Globals;
import com.xyz.apis.modules.api.service.EvUserService;
import com.xyz.apis.modules.comm.controller.AbstractController;
import com.xyz.apis.modules.comm.oauth2.TokenGenerator;
import com.xyz.apis.repository.RedisRepository;
import com.xyz.apis.repository.UserInfoRepository;
import com.xyz.apis.repository.UserTokenRepository;
import com.xyz.apis.repository.VerifyCodeRepository;
import com.xyz.apis.utils.CommonRateLimiterManager;
import io.swagger.annotations.*;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;


@Api(description = "登录、验证码接口",tags = "登录、密码接口")
@RestController
@RequestMapping(value = "/api", params = { "ver="+ Globals.version})
public class ApiLoginController extends AbstractController {

    @Autowired
    private EvUserService evUserService;

    @Autowired
    private UserTokenRepository userTokenRepository;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private VerifyCodeRepository verifyCodeRepository;

    @Autowired
    private RedisRepository redisRepository;


    @Autowired
    Globals globals;



    /**
     * 用户注册
     */
    @ApiOperation(value="扫码用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inviteCode", value = "邀请码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/userregist",method = RequestMethod.POST)
    @Idempotent(value = 60)
    public Rest userregist(@RequestParam(value="inviteCode",required = true) String inviteCode,
                           @RequestParam(value="password",required = true) String password){
        if(CommonUtil.isBlank(inviteCode)){
            return Rest.error("邀请码不能为空");
        }
        if(CommonUtil.isBlank(password)){
            return Rest.error("密码不能为空");
        }
        EvUserEntity pidEvUserEntity = evUserService.queryByPhone(null, Long.valueOf(inviteCode));
        if(null==pidEvUserEntity){
            return Rest.error("邀请码错误");
        }
        //用户没注册，注册并返回用户数据，并直接激活用户
        EvUserEntity userEntity = new EvUserEntity();
        userEntity.setPid(Long.valueOf(inviteCode));
        userEntity.setGpid(pidEvUserEntity.getGpid());
        userEntity.setPhone(TokenGenerator.generateValue());
        userEntity.setAccount(TokenGenerator.generateValue());
        userEntity.setNickname(TokenGenerator.generateValue());
        String salt = RandomStringUtils.randomAlphanumeric(20);     //sha256加密
        userEntity.setPassword(new Sha256Hash(password, salt).toHex());
        userEntity.setSalt(salt);
        try{
            EvUserEntity dbUserEntity = evUserService.insertUserData(userEntity,pidEvUserEntity);
            //serverService.bingUserToSp(dbUserEntity,pidEvUserEntity);
            return Rest.ok(dbUserEntity);
        }catch (Exception e){
            e.printStackTrace();
            return Rest.error("注册异常");
        }
    }

    /**
     * 用户注册
     */
    @ApiOperation(value="用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "verifyCode", value = "验证码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inviteCode", value = "邀请码", required = false, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public R regist(@RequestParam(value="phone") String phone,
                    @RequestParam(value="password") String password,
                    @RequestParam(value="verifyCode") String verifyCode,
                    @RequestParam(value="inviteCode",required = false) String inviteCode){

        if(1==1){
            return R.error().put("msg","已关闭注册，请直接下载APP");
        }

        //校验手机号码
        if(!CommonUtil.isPhone(phone)){
            return R.error().put("msg", "手机号码格式不正确");
        }
        if(CommonUtil.isBlank(password)){
            return R.error().put("msg", "密码不能为空");
        }
        if(6>password.length()){
            return R.error().put("msg", "密码长度不能小于6位");
        }
        if(CommonUtil.isBlank(verifyCode)){
            return R.error().put("msg", "验证码不能为空");
        }

        String nickname = "用户".concat(phone.substring(0,3)).concat(phone.substring(7,11));
        if(verifyCode.equals(verifyCodeRepository.getVerifyCode(phone)) || verifyCode.equals("010101")){
            //检查手机号是否已被注册
            EvUserEntity userEntity = evUserService.queryByPhoneAccount(phone,phone,null);
            if(userEntity != null && !CommonUtil.isBlank(userEntity.getPhone())){
                return R.error("手机号码已注册，请登录");
            }
            userEntity = new EvUserEntity();
            EvUserEntity pidEntity = null;
            if(!CommonUtil.isBlank(inviteCode)){
                pidEntity = evUserService.queryByPhone(null,Long.valueOf(inviteCode));
                if(pidEntity == null){
                    return R.error("邀请码错误");
                }
                userEntity.setPid(Long.valueOf(inviteCode));
                userEntity.setGpid(pidEntity.getGpid());
            }
            //用户没注册，注册并返回用户数据，并直接激活用户
            userEntity.setPhone(phone);
            userEntity.setAccount(phone);
            userEntity.setNickname(nickname);
            String salt = RandomStringUtils.randomAlphanumeric(20);     //sha256加密
            userEntity.setPassword(new Sha256Hash(password, salt).toHex());

            userEntity.setSalt(salt);
            try{
                int flag = evUserService.insertData(userEntity,pidEntity);
                if(0>=flag){
                    return R.error("注册失败");
                }
            }catch (Exception e){
                logger.error("注册异常, phone:{}", phone, e);
                return R.error("注册异常");
            }
            verifyCodeRepository.deleteVerifyCode(phone); // 这里也可以不移除，设置Redis过期时间为60s

            //处理是否有代理商
                if(null!=pidEntity){
                //serverService.bingUserToSp(userEntity,pidEntity);
                }

            return R.ok("注册成功");
        }
        return R.error().put("msg","验证码错误");
    }

    /**
     * 用户app登录、注册
     */
    @ApiOperation(value="用户app登录2", notes="用户app登录2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/ulogin",method = RequestMethod.POST)
    public Rest<EvUserEntity> ulogin(@RequestParam(value="phone",required = true) String phone,
                                     @RequestParam(value="password",required = true) String password){
        EvUserEntity evUserEntity = evUserService.queryByPhone(phone, null);
        if(null==evUserEntity){
            return Rest.error("账号不存在");
        }
        if(null!=evUserEntity && !new Sha256Hash(password, evUserEntity.getSalt()).toHex().equals(evUserEntity.getPassword())){
            return Rest.error("账号或密码不正确");
        }
        if (null==evUserEntity.getStatus()||evUserEntity.getStatus().intValue() == 0 ) {
            return Rest.error("账号已被注销,请联系管理员");
        }
        evUserEntity.setPassword(null);
        evUserEntity.setSalt(null);
        evUserEntity.setToken(TokenGenerator.generateValue());
        //保存token
        userTokenRepository.setToken(evUserEntity);
        userInfoRepository.setUserInfo(evUserEntity);
        return Rest.ok(evUserEntity);
    }

    /**
     * 用户app登录、注册
     */
    @ApiOperation(value="用户app登录", notes="用户app登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "latlng", value = "经纬度坐标", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "areaCode", value = "城市代码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Rest<EvUserEntity> login(@RequestParam(value="phone",required = true) String phone,
                                    @RequestParam(value="password",required = true) String password,
                                    @RequestParam(value="latlng",required = true) String latlng,
                                    @RequestParam(value="areaCode",required = true) String areaCode){
        if(CommonUtil.isBlank(phone)){
            return Rest.error("手机号码为空");
        }
        if(CommonUtil.isBlank(password)){
            return Rest.error("密码为空");
        }
        //phone 2020-06-29 16:51:22 增加了账号体系，为了兼容前端这里手机号，实质是账号
        //用户手动注册时，默认将手机号设置为账号，来做兼容处理
        EvUserEntity userEntity = evUserService.queryByPhoneAccount(null,phone,null);
        if (userEntity == null) {
            return Rest.error("登录失败，用户不存在");
        }
        if(CommonUtil.isBlank(userEntity.getPassword())||CommonUtil.isBlank(new Sha256Hash(password, userEntity.getSalt()).toHex())||
                !(new Sha256Hash(password, userEntity.getSalt()).toHex()).equals(userEntity.getPassword())) {
            return Rest.error("账号或密码不正确");
        }

        if (null==userEntity.getStatus()||userEntity.getStatus().intValue() == 0 ) {
            return Rest.error("账号已被注销,请联系管理员");
        }
        userTokenRepository.deleteTokenByAgainLogin(userEntity.getUserid());
        userEntity.setPassword(null);
        userEntity.setSalt(null);
        userEntity.setToken(TokenGenerator.generateValue());
        //保存token
        userTokenRepository.setToken(userEntity);
        userInfoRepository.setUserInfo(userEntity);
        if(null!=userEntity && 0==userEntity.getFirst().intValue()){
            //首次登陆，确认代理商关系
            userEntity.setReserved1(latlng);
            userEntity.setReserved2(areaCode);
            try {
                //serverService.loginBingUserToSp(userEntity);
            }catch (Exception e){
                logger.error("用户第一次登陆ERROR:{},info:{},时间戳:{}", e, JSONObject.toJSONString(userEntity),System.currentTimeMillis());
            }

        }


        return Rest.ok(userEntity);
    }

    @ApiOperation(value = "忘记密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userverifyCode", value = "验证码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/changepassword", method = RequestMethod.POST)
    public Rest changepassword(@RequestParam(value = "phone") String phone, @RequestParam(value = "userverifyCode") String userverifyCode,
                               @RequestParam(value = "password") String password){
        if(CommonUtil.isBlank(phone)){
            return Rest.error("手机号码为空");
        }
        if(11!=phone.length()){
            return Rest.error("手机号码格式不正确");
        }
        if(CommonUtil.isBlank(userverifyCode)){
            return Rest.error("验证码为空");
        }
//        VerifyCodeEntity v = (VerifyCodeEntity)TokenDBMap.verifyCodeMap.get(phone);
//        String code=v.getCode();
//        if(180000<System.currentTimeMillis()-v.getTime()){
//            return R.error("验证码过期");
//        }
        String verifyCode = verifyCodeRepository.getVerifyCode(Globals.verifycodeByPwd.concat(phone));

        if(CommonUtil.isBlank(password)){
            return Rest.error("密码为空");
        }

        if(6>password.length()){
            return Rest.error("密码必须大于等于6位");
        }
        //String verifyCode = TokenDBMap.verifyCodeMap.get(phone).toString();
        if (CommonUtil.isBlank(userverifyCode)||(!userverifyCode.equals(verifyCode)&& !userverifyCode.equals("010101"))) {
            return Rest.error("验证码不正确");
        }
        evUserService.changePassword(null,phone, password);
        verifyCodeRepository.deleteVerifyCode(Globals.verifycodeByPwd.concat(phone));
        return Rest.ok();

    }
    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "旧密码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "newpassword", value = "新密码", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/updatepassword", method = RequestMethod.POST)
    public Rest updatepassword(@RequestParam(value = "password") String password, @RequestParam(value = "newpassword") String newpassword, @RequestParam(value = "token") String token ){
        if(CommonUtil.isBlank(password)||CommonUtil.isBlank(newpassword)){
            return Rest.error("密码为空");
        }
        if(6>newpassword.length()){
            return Rest.error("新密码必须大于等于6位");
        }
        String msg = evUserService.updatepassword(getUserId(),password, newpassword);

        return CommonUtil.isBlank(msg)?Rest.ok("修改成功"):Rest.error(msg);

    }

    @ApiOperation(value = "设置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "新密码", required = true, dataType = "String", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/setuppassword", method = RequestMethod.POST)
    public Rest setuppassword(@RequestParam(value = "token") String token, @RequestParam(value = "password")String password){
        if(CommonUtil.isBlank(password)){
            return Rest.error("密码为空");
        }
        if(6>password.length()){
            return Rest.error("密码必须大于等于6位");
        }
        String msg = evUserService.changePassword(getUserId(),null, password);
        if(!CommonUtil.isBlank(msg)){
            return Rest.error(msg);
        }
        return Rest.ok("设置成功");

    }

    /**
     * 获取验证码
     */
    @ApiOperation(value = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getverifycode", method = RequestMethod.POST)
    public Rest getVerifyCode(@RequestParam(value = "phone") String phone) {

        if(1==1){
            return Rest.error("已关闭注册，请直接下载APP");
        }

        if (phone == null && "".equals(phone)) {
            return Rest.error("手机号码不能为空");
        }
        EvUserEntity evUserEntity = evUserService.queryByPhone(phone,null);
        if (null!=evUserEntity&&!CommonUtil.isBlank(evUserEntity.getPhone())) {
            return Rest.error("手机号码已注册");
        }
        //判断该手机号码的上一个验证码获取时间是否超过1分钟或者没使用
        boolean istrue = CommonRateLimiterManager.enter(phone);
        if (!istrue) {
            return Rest.error("验证码获取太频繁");
        }
        //生成6位验证码
        int verifyCode = (int) ((Math.random() * 9 + 1) * 100000);
        boolean isSend = true;//OtherSMSUtils.send_sms(phone,String.format(globals.verifycodevalue,verifyCode));
        //boolean isSend = AliSMSUtils.aliSenSms(phone,String.valueOf(verifyCode));
        if(isSend){
            //redis缓存该手机号码的验证码
            verifyCodeRepository.setVerifyCode(phone,String.valueOf(verifyCode));
            return Rest.ok();
        }
        return Rest.error("获取短信验证失败");
    }

    /**
     * 忘记密码获取验证码
     */
    @ApiOperation(value = "忘记密码获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getverifycodeByPwd", method = RequestMethod.POST)
    public Rest getverifycodeByPwd(@RequestParam(value = "phone") String phone) {

        if (phone == null && "".equals(phone)) {
            return Rest.error("手机号码不能为空");
        }
        EvUserEntity evUserEntity = evUserService.queryByPhone(phone,null);
        if (null==evUserEntity) {
            return Rest.error("手机号码未被注册");
        }
        //判断该手机号码的上一个验证码获取时间是否超过1分钟或者没使用
        boolean istrue = CommonRateLimiterManager.enter(phone);
        if (!istrue) {
            return Rest.error("验证码获取太频繁");
        }
        //生成6位验证码
        int verifyCode = (int) ((Math.random() * 9 + 1) * 100000);
        boolean isSend = true;//OtherSMSUtils.send_sms(phone,String.format(globals.verifycodevalue,verifyCode));
        //boolean isSend = AliSMSUtils.aliSenSms(phone,String.valueOf(verifyCode));
        if(isSend){
            //redis缓存该手机号码的验证码
            verifyCodeRepository.setVerifyCode(Globals.verifycodeByPwd.concat(phone),String.valueOf(verifyCode));
            return Rest.ok();
        }
        return Rest.error("获取短信验证失败");
    }



    /**
     * 退出登录
     */
    @ApiOperation(value="退出登录", notes="退出登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/loginout",method = RequestMethod.POST)
    public Rest loginout(@RequestParam(value="token",required = true) String token){

        //退出登录，删除token
        userTokenRepository.deleteToken(token,getUserId());
        return Rest.ok();
    }

    /**
     * 获取二维码
     */
    @ApiOperation(value="获取二维码", notes="获取二维码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getQRCode",method = RequestMethod.POST)
    public Rest getQRCode(){
        String uuid ="tcft".concat(CommonUtil.UUID());
        Boolean istrue = redisRepository.setQRCodeValue(uuid, "temp000");

        return istrue?Rest.ok(uuid):Rest.error("error");
    }

    /**
     * 扫描登陆
     */
    @ApiOperation(value="扫描登陆", notes="扫描登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qrCode", value = "qrcode", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getScanLogin",method = RequestMethod.POST)
    public Rest getScanLogin(@RequestParam(value = "token") String token, @RequestParam(value = "qrCode")String qrCode){

        String qrvalue = redisRepository.getQRCodeValue(qrCode);
        if(CommonUtil.isBlank(qrvalue)){
            return Rest.error("该二维码已经失效,请重新获取");
        }
        EvUserEntity userEntity = getUser();
        if(null==userEntity){
            return Rest.error("token失败");
        }
        userTokenRepository.deleteToken(token,userEntity.getUserid());
        userEntity.setPassword(null);
        userEntity.setSalt(null);
        String newtoken = TokenGenerator.generateValue();
        userEntity.setToken(newtoken);
        //保存token
        userTokenRepository.setToken(userEntity);
        userInfoRepository.setUserInfo(userEntity);
        redisRepository.setQRCodeValue(qrCode, newtoken);
        return Rest.ok("ok");
    }

    /**
     * 获取二维码登陆信息
     */
    @ApiOperation(value="获取二维码登陆信息", notes="获取二维码登陆信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "qrCode", value = "qrcode", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/getScanQRCode",method = RequestMethod.POST)
    public Rest<EvUserEntity> getScanQRCode(@RequestParam(value = "qrCode")String qrCode){

        String token = redisRepository.getQRCodeValue(qrCode);
        if(CommonUtil.isBlank(token)){
            return Rest.error("该二维码已经失效,请重新获取");
        }
        if(token=="temp000"){
            return Rest.error("请扫描登陆");
        }
        EvUserEntity entity = userTokenRepository.getToken(token);
        if(null==entity||!CommonUtil.moreThanZero(entity.getUserid())){
            return Rest.error("登陆失败");
        }
        entity.setConsumeJifen(BigDecimal.ZERO);
        entity.setJifen(BigDecimal.ZERO);
        return Rest.ok(entity);
    }


    /**
     * 用户PC登录
     */
    @ApiOperation(value="用户PC登录", notes="用户PC登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ver", value = "接口版本号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query")})
    @ApiResponses({
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })
    @RequestMapping(value = "/pcLogin",method = RequestMethod.POST)
    public Rest<EvUserEntity> pcLogin(@RequestParam(value="phone",required = true) String phone,
                                      @RequestParam(value="password",required = true) String password){
        if(CommonUtil.isBlank(phone)){
            return Rest.error("手机号码为空");
        }
        if(CommonUtil.isBlank(password)){
            return Rest.error("密码为空");
        }
        EvUserEntity userEntity = evUserService.queryByPhone(phone,null);
        if (userEntity == null) {
            return Rest.error("登录失败，用户不存在");
        }
        if(CommonUtil.isBlank(userEntity.getPassword())||CommonUtil.isBlank(new Sha256Hash(password, userEntity.getSalt()).toHex())||
                !(new Sha256Hash(password, userEntity.getSalt()).toHex()).equals(userEntity.getPassword())) {
            return Rest.error("账号或密码不正确");
        }

        if (null==userEntity.getStatus()||userEntity.getStatus().intValue() == 0 ) {
            return Rest.error("账号已被注销,请联系管理员");
        }
        logger.info("user={}|使用pc登陆|",userEntity.getUserid());
        userTokenRepository.deleteTokenByAgainLogin(userEntity.getUserid());
        userEntity.setPassword(null);
        userEntity.setSalt(null);
        userEntity.setToken(TokenGenerator.generateValue());
        //保存token
        userTokenRepository.setToken(userEntity);
        userInfoRepository.setUserInfo(userEntity);
        //evUserService.updateUserinfo()
        userEntity.setConsumeJifen(BigDecimal.ZERO);
        userEntity.setJifen(BigDecimal.ZERO);
        return Rest.ok(userEntity);
    }





}
